package com.jjc.MyWebApp.directive;

import com.jfinal.kit.JsonKit;
import com.jfinal.log.Log;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.stat.Scope;
import com.jjc.MyWebApp.Constant.WebContextConstant;
import com.jjc.MyWebApp.Model.User;
import java.io.Writer;

/**
 * 获取当前用户
 * Created by jiajianchang on 2017/5/13.
 */
public class currentUser extends Directive {

    private static final Log log=Log.getLog(currentUser.class);

    public void exec(Env env, Scope scope, Writer writer) {

        User user = (User) scope.getLocal(WebContextConstant.current_User);
        if (user == null) return;
        if(this.exprList.length()==0){
            write(writer, JsonKit.toJson(user).replace("\"",""));
        }

        if (this.exprList.length()==1){
            Object Key = this.exprList.eval(scope);
            Key = user.getStr(Key.toString());
            if (Key == null) return;
            write(writer, Key.toString());
        }

        if (this.exprList.length()>1){
            log.error("user自定义指令入参错误!!! 请检查!!!");
        }



    }
}
